//
// Created by 11510 on 2025/10/2.
//

#include "AcquireCommand.h"

xclib_client::AcquireCommand::AcquireCommand(CommandContext ctx) : ICommand("acquire", ctx) {};

std::optional<std::future<int>> xclib_client::AcquireCommand::run(const std::vector<std::string> &args, bool wait) {
    cxxopts::Options o("acquire", "acquire");
    o.add_options()
    ("u,unitmap", "unitmap", cxxopts::value<std::string>()->default_value(""))
    ("n,nums", "nums", cxxopts::value<int>()->default_value("1"))
    ("help","help");
    try {
        auto r = parseOptions(o, args);
        if (r.count("help")) {
            std::cout << o.help() << "\n";
            return std::nullopt;
        }
        std::string u = r["unitmap"].as<std::string>();
        int n = r["nums"].as<int>();
        auto task = [u, n]() {
            std::cout << "acquire " << n << " from '" << u << "'...\n";
            return 0;
        };
        if (!wait)
            return make_optional(std::async(std::launch::async, task));
        else {
            task();
            return std::nullopt;
        }
    } catch (const cxxopts::exceptions::exception &e) {
        std::cerr << "acquire: " << e.what() << "\n";
        return std::nullopt;
    }
}

std::string xclib_client::AcquireCommand::shortHelp() const {
    return "acquire -u <unitmap> -n <nums>";
}

std::string xclib_client::AcquireCommand::longHelp() const {
    return "acquire -u <unitmap> -n <nums>\n acquire samples";
}